<?php
/**
 * This is main controller for LilPlugins. Each plugin must extend this controller in
 * as controllers/lli_{plugin_name}_controller.php
 *
 * @copyright     Copyright 2008-2010, LilCake.net
 * @link          http://lilcake.net LilCake.net
 * @package       lil
 * @subpackage    lil.controllers
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::import('Lib', 'Lil.LilPluginRegistry');
/**
 * LilPluginController class
 *
 * @uses          AppController
 * @package       lil
 * @subpackage    lil.controllers
 */
class LilPluginController extends Controller {
/**
 * name property
 *
 * @var string 'LilPlugin'
 * @access public
 */
	var $name = 'LilPlugin';
/**
 * autoRender property
 *
 * @var boolean
 * @access public
 */
	var $autoRender = NULL;
/**
 * autoLayout property
 *
 * @var boolean
 * @access public
 */
	var $autoLayout = NULL;
/**
 * uses property
 *
 * @var array
 * @access public
 */
	var $uses = NULL;
/**
 * components property
 *
 * @var array
 * @access public
 */
	var $components = array('Session');
/**
 * handlers property
 *
 * @var array
 * @access public
 */
	var $handlers = array('initialize' => array('function' => '_initialize', 'params' => array()));
/**
 * __construct method
 *
 * @access public
 * @return void
 */
	function __construct() {
		// required for sending emails from console
		if (!class_exists('Dispatcher')) require CAKE . 'dispatcher.php';
		
		$Dispatcher = new Dispatcher();
		$this->params = $Dispatcher->parseParams($Dispatcher->getUrl());
		$this->base = $Dispatcher->baseUrl();
		$this->webroot = $Dispatcher->webroot;
		unset($Dispatcher);
		
		parent::__construct();
	}
/**
 * attachHandler method
 *
 * @param string $action
 * @param string $function_name
 * @param mixed $params
 * @access public
 * @return void
 */
	function attachHandler($action, $function_name, $params = array()) {
		$this->handlers[$action] = array('function' => $function_name, 'params' => (array)$params);
	}
/**
 * getAdminAreaId method
 *
 * This functions returns current blog id in administration.
 *
 * @access public
 * @return int
 */
	function getAdminAreaId() {	
		if ($data = $this->getAdminArea()) {
			return (isset($data['Area']['id'])) ? $data['Area']['id'] : true;
		} else return null;
	}
/**
 * getAdminAreaSlug method
 *
 * This functions returns current blog slug in administration.
 *
 * @access public
 * @return int
 */
	function getAdminAreaSlug() {
		if ($data = $this->getAdminArea()) {
			return $data['Area'][Configure::read('Lil.areaSlugField')];
		} else return null;
	}
/**
 * getAdminArea method
 *
 * This functions returns current blog in administration.
 *
 * @access public
 * @return array
 */
	function getAdminArea() {
		if (!empty($this->Session) && ($a = $this->Session->read('admin.area'))) {
			return $a;
		} else {
			return null;
		}
	}
/**
 * clearAdminArea method
 *
 * This functions clears admin area.
 *
 * @access public
 * @return void
 */
	function clearAdminArea() {
		$this->Session->delete('admin.area');
	}
/**
 * insertIntoArray method
 *
 * Insert a new element into array
 *
 * @param array $dest
 * @param array $element
 * @param array $options
 * @access public
 * @return void
 */
	function insertIntoArray(&$dest, $element, $options) {
		if (isset($options['after'])) {
			$panels = array_keys($dest);
			$i = 0;
			for ($i = 0; $i < sizeof($dest); $i++) {
				if ($panels[$i] == $options['after']) break;
			}
			
			if ($panels[$i] == $options['after']) {
				// do this to preserve array keys
				$dest = 
					array_slice($dest, 0, $i + 1, true) + 
					$element + 
					array_slice($dest, $i + 1, sizeof($dest) - $i, true);
			}
		} else if (isset($options['before'])) {
			$panels = array_keys($dest);
			$i = 0;
			for ($i = 0; $i < sizeof($dest); $i++) {
				if ($panels[$i] == $options['before']) break;
			}
			
			if ($panels[$i] == $options['before']) {
				// do this to preserve array keys
				$dest = 
					array_slice($dest, 0, $i, true) + 
					$element + 
					array_slice($dest, $i, sizeof($dest) - $i, true);
			}
		} else if (isset($options['end'])) {
			$dest = $dest + $element;
		}
	}
/**
 * sidebarInsertPanel method
 *
 * Insert a new sidebar panel with specific options
 *
 * @param array $options
 * @param array $panel Sidebar panel
 * @access public
 * @return void
 */
	function sidebarInsertPanel(&$sidebar, $options, $panel) {
		$this->insertIntoArray($sidebar, $panel, $options);
	}
/**
 * user method
 *
 * Returns current user
 *
 * @param mixed $key
 * @access public
 * @return mixed
 */
	function user($key = null) {
		static $session = null;
		if (empty($session)) $session = new CakeSession();
		
		if ($session->valid(Configure::read('Lil.authSessionKey'))) {
			if ($key == null) {
				return array(
					Configure::read('Lil.authModel') => 
						$session->read(Configure::read('Lil.authSessionKey'))
				);
			} else {
				$user = $session->read(Configure::read('Lil.authSessionKey'));
				if (isset($user[$key])) return $user[$key];
			}
		}
		return null;
	}
/**
 * isAdmin method
 *
 * Returns user is an administrator. 
 * If there is no user specified, current user admin status is returned.
 *
 * @param mixed $user_id
 * @access public
 * @return mixed
 */
	function isAdmin($user_id = null) {
		if (empty($user_id) || ($user_id == $this->user('id'))) {
			static $session = null;
			if (empty($session)) $session = new CakeSession();
			
			if ($session->valid(Configure::read('Lil.authSessionKey')) &&
				$user = $session->read(Configure::read('Lil.authSessionKey')))
			{
				return (bool)$user[Configure::read('Lil.userAdminField')];
			}
			return false;
		} else {
			$User =& ClassRegistry::init('Lil.User');
			return (bool)$User->field(
				Configure::read('Lil.userAdminField'),
				array('User.id' => $user_id)
			);
		}
	}
}